#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include "logger.h"
#include "client_sqlite.h"

typedef struct pack_socket{
        char    T_time[128];
        char    name[128];
        float   temperature;
}pack_info;



//socket()初始化、connect()连接
int client_connect( char *ip,int listen_port)
{
        struct sockaddr_in      serv_addr;
        //char                    *ip;
        //int                     listen_port;
        int                     rv=-1;
        int                     conn_fd=-1;

        conn_fd=socket(AF_INET,SOCK_STREAM,0);
                if(conn_fd<0){
                        printf("create socket failure:%s\n",strerror(errno));
                        close(conn_fd);
                        return -1;
                }
                printf("create socket ok\n");
                memset(&serv_addr,0,sizeof(serv_addr));
                serv_addr.sin_family=AF_INET;
                serv_addr.sin_port=htons(listen_port);
                inet_aton(ip,&serv_addr.sin_addr);

                if(connect(conn_fd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0){
                        printf("connect to server failure:%s\n",strerror(errno));
                        close(conn_fd);
                 }
        return conn_fd;
}

int pack_data(pack_info data,char *buf,int size){

        if(!data.name||!data.T_time||!data.temperature){
                printf("get number failure\n");
                return -1;
        }
        snprintf(buf,size,"%s/%s/%f",data.name,data.T_time,data.temperature);
        return 0;

}

